package com.yanqu.road.server.gameplayer.module.recharge;

import com.yanqu.data.analytics.constant.EventDataConstants;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.recharge.GoldTokenOrder;
import com.yanqu.road.logic.bussiness.recharge.GoldTokenBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.recharge.RechargeProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.maidian.DefaultEventTraceAction;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

public class GoldTokenRechargeModule extends GeneralModule {
    private List<GoldTokenOrder> goldTokenOrderList;

    public GoldTokenRechargeModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        goldTokenOrderList = GoldTokenBussiness.getGoldTokenOrderList(player.getUserId());
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        return true;
    }

    public synchronized void loginLogic() {
        if (null != goldTokenOrderList && goldTokenOrderList.size() > 0) {
            for (GoldTokenOrder order : goldTokenOrderList) {
                if (order.getOrderStatus() == 0) {
                    rechargeSuccess(order, false);
                }
            }
        }
    }

    public synchronized int rechargeSuccess(GoldTokenOrder goldTokenOrder, boolean isAddToList) {
        for (GoldTokenOrder order : goldTokenOrderList) {
            if (order.getOrderNo().equals(goldTokenOrder.getOrderNo())) {
                if(1 == order.getOrderStatus()) {
                    log.error("通宝已经到账，orderNo:{},userId:{}", goldTokenOrder.getOrderNo(), player.getUserId());
                    return GameErrorCode.E_RECHARGE_FAIL;
                }
            }
        }
        log.info("通宝订单正在到账，orderNo:{},userId:{}", goldTokenOrder.getOrderNo(), player.getUserId());
        goldTokenOrder.setUpdateTime(new Date());
        goldTokenOrder.setOrderStatus(1);
        if (!GoldTokenBussiness.updateOrderStatus(goldTokenOrder)) {
            return GameErrorCode.E_RECHARGE_FAIL;
        }

        Property rechargeReward = new Property(eItemId.GOLD_TOKEN.getValue(), BigInteger.valueOf(goldTokenOrder.getMoney()));
        String goldTokenReward = PropertyHelper.parsePropertyToString(rechargeReward);
        long ingotsCount = goldTokenOrder.getMoney();     //返利10%元宝
        rechargeReward.addProperty(new Property(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(ingotsCount)));
        player.getModule(CurrencyModule.class).addCurrency(rechargeReward, eLogMoneyType.Recharge, eLogMoneyType.ReChargeGet);
        notifyClientRechargeSuccess(goldTokenReward);

        //添加到充值列表中
        if (isAddToList) {
            goldTokenOrderList.add(goldTokenOrder);
        }

        //通宝钱包 埋点
        try {
            DefaultEventTraceAction traceAction = new DefaultEventTraceAction(EventDataConstants.Order.ORDER_FINISH, player);
            //防止时间差
            if (goldTokenOrder.getUpdateTime() != null) {
                traceAction.build("#time", DateHelper.getCurrentDateTimeString(goldTokenOrder.getUpdateTime().getTime()));
            }
            traceAction.build(EventDataConstants.Order.ORDER_SDKID, goldTokenOrder.getOrderNo());
            traceAction.build(EventDataConstants.Order.ORDER_FINISH_PAY_TYPE, "CNY");
            traceAction.build(EventDataConstants.Order.ORDER_FINISH_PAY_AMOUNT, goldTokenOrder.getMoney() * 100);
            traceAction.build("pay_reason_markid", 2);
            traceAction.trace();
        }catch (Exception e){
            DataAnalyticsMgr.getLogger().error("", e);
        }
        return 0;
    }

    public void notifyClientRechargeSuccess(String reward){
        if(StringUtils.isNullOrEmpty(reward)){
            return;
        }
        RechargeProto.GoldTokenRechargeSuccessMsg.Builder msg = RechargeProto.GoldTokenRechargeSuccessMsg.newBuilder();
        msg.setReward(reward);
        player.sendPacket(Protocol.U_GOLD_TOKEN_RECHARGE_SUCCESS, msg);
    }
}
